﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using SpeakupBackend.Classes;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Globalization;
using System.Windows.Media;
using SpeakupBackend;
using System.ComponentModel;

namespace Frontend
{
    class TodoGridEntry
    {
        TextBlock RemoveTextBlock;
        TextBlock TimeTextBlock;
        TextBlock NameTextBlock;
        TextBlock ThemeTextBlock;
        TextBlock StatusTextBlock;
        TextBlock ActionTextBlock;
        TextBlock IsApplicableActionTextBlock;

        Border IsApplicableActionBorder;

        List<TextBlock> TextBlocks = new List<TextBlock>();


        Class Class;

        DelegateProxy<PropertyChangedEventHandler> PropertyChangedProxy =
                new DelegateProxy<PropertyChangedEventHandler>();

        public TodoGridEntry(Class speakupClass)
        {
            Class = speakupClass;

            speakupClass.PropertyChanged += PropertyChangedProxy.ProxyConsumer;
            PropertyChangedProxy.ProxySource += Class_PropertyChanged;

            SetupControls();
            UpdateControls();
        }

        void Class_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            UpdateControls();
        }

        void SetupControls()
        {
            RemoveTextBlock = new TextBlock();
            TimeTextBlock = new TextBlock();
            NameTextBlock = new TextBlock();
            ThemeTextBlock = new TextBlock();
            StatusTextBlock = new TextBlock();
            ActionTextBlock = new TextBlock();
            IsApplicableActionTextBlock = new TextBlock();

            IsApplicableActionBorder = new Border();
            IsApplicableActionBorder.Margin = new Thickness(0.75);
            IsApplicableActionBorder.Child = IsApplicableActionTextBlock;
            
            TextBlocks.Add(RemoveTextBlock);
            TextBlocks.Add(TimeTextBlock);
            TextBlocks.Add(NameTextBlock);
            TextBlocks.Add(ThemeTextBlock);
            TextBlocks.Add(StatusTextBlock);
            TextBlocks.Add(ActionTextBlock);
            TextBlocks.Add(IsApplicableActionTextBlock);

            foreach (TextBlock txtBlock in TextBlocks)
            {
                txtBlock.TextAlignment = TextAlignment.Center;
                txtBlock.TextWrapping = TextWrapping.Wrap;
                txtBlock.VerticalAlignment = VerticalAlignment.Center;
                txtBlock.Margin = new Thickness(5, 2, 5, 2);
            }            
        }

        void UpdateControls()
        {
            //remove
            Hyperlink removeLink = new Hyperlink(new Run("(remove)"));
            removeLink.Click += (s, e) => Class.DiscardAction();

            RemoveTextBlock.Inlines.Clear();
            RemoveTextBlock.Inlines.Add(removeLink);            

            //time
            TimeTextBlock.Inlines.Clear();

            string date = Class.Time.Date.ToString("dd MMM yyyy", CultureInfo.InvariantCulture);
            string time = String.Format(@"{0:hh\:mm} - {1:hh\:mm}", Class.Time.Begin, Class.Time.End);

            TimeTextBlock.Inlines.Add(date);
            TimeTextBlock.Inlines.Add(new LineBreak());
            TimeTextBlock.Inlines.Add(time);

            //name
            NameTextBlock.Inlines.Clear();

            string name = String.Format("{0} {1}", Class.Name.Name, Class.Name.Kind);
            NameTextBlock.Inlines.Add(name);

            //theme
            ThemeTextBlock.Inlines.Clear();

            if (Class.Theme == null)
            {
                ThemeTextBlock.Inlines.Add("<no theme>");
            }
            else
            {
                if (Class.Theme.IsAttended)
                {
                    Run themeRun = new Run(Class.Theme.Name);
                    themeRun.TextDecorations = TextDecorations.Strikethrough;
                    ThemeTextBlock.Inlines.Add(themeRun);
                }
                else
                {
                    ThemeTextBlock.Inlines.Add(Class.Theme.Name);
                }
            }

            //status
            StatusTextBlock.Inlines.Clear();
            StatusTextBlock.Inlines.Add(Class.CurrentStatus.ToString());

            //action
            ActionTextBlock.Inlines.Clear();
            string action = Class.Action.ToString();
            if (Class.Action == ClassAction.Reserve)
            {
                action = "Reserve";
            }
            else if(Class.Action == ClassAction.CancelReservation)
            {
                action = "Cancel";
            }
            ActionTextBlock.Inlines.Add(action);

            //is action applicable
            IsApplicableActionTextBlock.Inlines.Clear();

            string message;
            if (!Class.IsActionApplicable(out message))
            {
                IsApplicableActionBorder.Background = Brushes.LightPink;
            }
            else
            {
                message = "Yes";
                IsApplicableActionBorder.Background = Brushes.LightGreen;
            }
            IsApplicableActionTextBlock.Inlines.Add(message);           
        }
             
      
        public void AddToGrid(Grid grid, int row)
        {
            Grid.SetColumn(RemoveTextBlock, (int)TodoGridHeader.Remove);
            Grid.SetRow(RemoveTextBlock, row);

            Grid.SetColumn(TimeTextBlock, (int)TodoGridHeader.Time);
            Grid.SetRow(TimeTextBlock, row);

            Grid.SetColumn(NameTextBlock, (int)TodoGridHeader.Name);
            Grid.SetRow(NameTextBlock, row);

            Grid.SetColumn(ThemeTextBlock, (int)TodoGridHeader.Theme);
            Grid.SetRow(ThemeTextBlock, row);

            Grid.SetColumn(StatusTextBlock, (int)TodoGridHeader.Status);
            Grid.SetRow(StatusTextBlock, row);

            Grid.SetColumn(ActionTextBlock, (int)TodoGridHeader.Action);
            Grid.SetRow(ActionTextBlock, row);

            Grid.SetColumn(IsApplicableActionBorder, (int)TodoGridHeader.IsApplicable);
            Grid.SetRow(IsApplicableActionBorder, row);

            foreach (TextBlock txtBlock in TextBlocks)
            {
                if (txtBlock != IsApplicableActionTextBlock)
                {
                    grid.Children.Add(txtBlock);
                }
            }            

            grid.Children.Add(IsApplicableActionBorder);         
        }

        public void RemoveFromGrid(Grid grid)
        {
            foreach (TextBlock txtBlock in TextBlocks)
            {
                grid.Children.Remove(txtBlock);
            }
            grid.Children.Remove(IsApplicableActionBorder);          
        }
    }
}
